package com.group.seckill.controller;

import com.group.common.utils.R;
import com.group.seckill.service.SeckillService;
import com.group.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author lins
 * @date 2021/11/15 14:48
 */
@Controller
@RequestMapping("/seckill")
public class SeckillController {
    @Autowired
    SeckillService seckillService;

    @RequestMapping("/getCurrentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus(){
        List<SeckillSkuRedisTo> redisTos = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(redisTos);
    }
    @RequestMapping("/skuInfo/{skuId}")
    @ResponseBody
    public R getSkuInfoBySkuId(@PathVariable Long skuId){
        SeckillSkuRedisTo redisTo = seckillService.getSkuInfoBySkuId(skuId);
        return R.ok().setData(redisTo);
    }
    @RequestMapping("/seckill")
    public String seckill(
            @RequestParam String killId,
            @RequestParam String key,
            @RequestParam Integer num,
            Model model
    ){
        String orderSn = seckillService.seckill(killId, key, num);
        model.addAttribute("orderSn", orderSn);
        return "success";
    }
}
